/**
this file is the global direct js
**/
function Haaway(){
	this.author = 'haaway studio';
	this.version = '2012-4';
	this.urlrewrite = false;
	
	this.include = function(urls){
		if(typeof urls == 'string'){
			urls = [urls];
		}
		
		var jsfiles = new Array();
		var ext = '';
		for(var i=0; i<urls.length; i++){
			var url = urls[i];
			if(typeof url!='string')
				continue;
			var arr_url = url.split('.');
			ext = arr_url[arr_url.length-1];
			if(ext == 'js'){
				jsfiles.push(url);
			}else if(ext == 'css'){
				document.write('<link type="text/css" rel="stylesheet" href="' + url + '" />');
			}
		}
		for(var i=0; i<jsfiles.length; i++){
			document.write('<script src="' + jsfiles[i] + '"></script>');
		}
	};
	this.load =  function(url, param, callback, beforeSend){
		param = param || {};
		callback = callback || false;
		beforeSend = beforeSend || false;


		var result;
		if(this.urlrewrite == false){
			url = '/index.php' +url;
		}
		$.ajax({
			async	: callback ? true : false,
			beforeSend : function(XMLHttpRequest){
				if(beforeSend) beforeSend(XMLHttpRequest);
			},
			data	: param,
			dataType : 'json',
			success : function(data){
				result = data;
				if(callback && data){
					callback(data);
				}
			},
			timeout : 150000,
			type	: 'POST',
			url 	: url
		});

		return result;
		
	};
		
	this.startup = function(opts){
		if(typeof opts.urlrewrite !='undefined'){
			this.urlrewrite = opts.urlrewrite;
		}
//		console.debug('我操');
//		this.load('/common/index', '', function(data){
////			console.debug(data);
//		});
	}
	
	
}

Haaway.prototype = {
		
		
		alert	: function(message,callback,width,height,title){
			var title = title || '信息提示';
			var fn = callback || true;
			$.dialog({
				title: title,
				width:width,
				height:height,
			    icon: 'alert.gif',
			    content: message,
				lock: true,
				ok:fn,
				padding: 10,
				maxTop:true
			});
			
		},
		saved: function(message, fn, width, height, title, btn) {
			message = message || '保存数据成功！';
			title = title || '操作成功';
			$.dialog({title: title,width:width,height: height,icon: 'success.gif',content: message,ok: fn,button:btn,okVal: '确定',maxTop:true});
		}, 
		/**
		 * 询问信息
		 */
		confirm: function(message, callback, width, height,locks) {
			var title = "信息提示";
			var locks = locks || true;
			var fn = callback || false;
			$.dialog({content: message,width:width,height: height,title: title,icon: 'prompt.gif',ok:(function() {
				return function() {
					callback('ok');
				};
			})(),cancel:(function() {
				return function() {
					callback('no');
				};
			})(),lock:true,zIndex:1976,maxTop:true});
		},

		/**
		* 弹出窗
		* @param message 文本/iframe模式的URL
		* @param width 宽
		* @param height 高
		* @param title 标题
		* @param callback 回调
		* @param iframe 为空则为普通模式，否则填iframe的属性，示例：{id: 'iframe01'}
		* @param btn 自定义按钮，示例:[['是','yes'],['否','no'],['取消','cancel']]})
		**/ 
		win: function(message, width, height, title, callback, iframe, btn,fid,lock) {
			message = message || "url:"+iframe;
			fid = fid || 'iframid';
			if(message.indexOf(".htm") > -1){
			message = "url:" + message;
			}
			var parent_win = $.dialog({content: message,width:width,height: height,title: title,ok: callback,button:btn,lock:lock,padding: 0,id:fid});
			return parent_win;
		},
		/**
		 * 新解析 str_param = ['id','name'] 按传参顺序
		 */
		getUrlParams_p : function(purl, str_method, str_param){
			var url = purl || window.location.href;
			var paraObj = {};
			
			var method = str_method || 'add';
			var param = str_param || ['id'];
			if(url.indexOf(method+'/') < 0){
				return paraObj;
			}
			var a = url.split(method+'/');
			var a1 = a[1].split('/');
			for(var i=0; i<a1.length; i++){
				var a2=a1[i];
				paraObj[param[i]] = a2;
			}
			return paraObj;
		},
		/**
		 * 填充表单,匹配字段名字结尾的所有值
		 * {name:'xiaoming'} 匹配:name,old_name
		 *
	     * @param	formid    表单ID,不能为空
	     * @param   params    JSON格式,如: {name:'xiaoming',age:20} 也可传String,如:aa=bb&cc=dd
	     * $param   pre       前缀
		 **/
	    fillFormInput_p: function(formid,params,pre) {
	        if(!formid || !params) return;
	        pre = pre || '';
	        if(typeof(params)=='string') {
	            params = this.getUrlParams_p(params);
	        }
	        var checker = false;
	        for(var i in params){
	        	var v = params[i] ;
	        	if($('#'+formid+' input[name$='+i+']').length){
	        		if($('#'+formid+' input[name$='+i+']').attr("type")=="checkbox"){
	            		checker = v==1 ? true : false;
	            		$('#'+formid+' input[name$='+i+']').attr("checked",checker);
	            	}else{
	            		$('#'+formid+' input[name$='+i+']').val(v);
	            	}
	        	}
	            else if($('#'+pre+i).length)
	                $('#'+pre+i).val(v);
	        }

	    },
		
		/**
		 * 解析URL返回GET参数,可以手动传入如:aa=bb&cc=dd
		 **/
	    getUrlParams: function(purl) {
	        var url = purl || window.location.href;
	        var paraObj = {};
	        if(url.indexOf("?")<0){
	        	return paraObj;
	        }
			url = url.replace('#','');
	        var paraString = url.substring(url.indexOf("?")+1,url.length).split("&");
	        for (i=0; j=paraString[i]; i++){
	            paraObj[j.substring(0,j.indexOf("=")).toLowerCase()] = j.substring(j.indexOf("=")+1,j.length);
	        }
	        return paraObj;
	    },
		/**
		 * 填充表单,匹配字段名字结尾的所有值
		 * {name:'xiaoming'} 匹配:name,old_name
		 *
	     * @param	formid    表单ID,不能为空
	     * @param   params    JSON格式,如: {name:'xiaoming',age:20} 也可传String,如:aa=bb&cc=dd
	     * $param   pre       前缀
		 **/
	    fillFormInput: function(formid,params,pre) {
	        if(!formid || !params) return;
	        pre = pre || '';
	        if(typeof(params)=='string') {
	            params = this.getUrlParams(params);
	        }
	        var checker = false;
	        for(var i in params){
	        	var v = params[i] ;
	        	if($('#'+formid+' input[name$='+i+']').length){
	        		if($('#'+formid+' input[name$='+i+']').attr("type")=="checkbox"){
	            		checker = v==1 ? true : false;
	            		$('#'+formid+' input[name$='+i+']').attr("checked",checker);
	            	}else{
	            		$('#'+formid+' input[name$='+i+']').val(v);
	            	}
	        	}
	            else if($('#'+pre+i).length)
	                $('#'+pre+i).val(v);
	        }

	    }
};
var $h = new Haaway();


document.write('<link type="text/css" rel="stylesheet" href="/resources/scripts/blackbirdjs/blackbird.css" />');
document.write('<link type="text/css" rel="stylesheet" href="/resources/scripts/lhgdialog/skins/default.css" />');

$h.include([
            '/resources/scripts/blackbirdjs/blackbird.js',            
            '/resources/scripts/lhgdialog/lhgdialog.js'
]);
$h.startup({});